package com.laity.event.publisher;

import com.laity.event.CustomEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: Laity
 * @Project: JavaLaity
 * @Package: com.laity.event.publisher.MyPublisherBean
 * @Date: 2023年11月21日 11:09
 * @Description: 事件发布者
 */
@Service
public class MyPublisherBean {

    @Resource
    private ApplicationEventPublisher eventPublisher;

//    @Override
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.eventPublisher = eventPublisher;
//    }
//    public void publishMyEvent() {
//        // 创建并发布自定义事件
//        CustomEvent event = new CustomEvent(this, "msg publisher");
//        eventPublisher.publishEvent(event);
//    }

    @EventListener(ApplicationReadyEvent.class)
    public void publishEvent() {
        CustomEvent event = new CustomEvent(this, "msg publisher test");
        System.out.println(Thread.currentThread().getName() + "-1");
        eventPublisher.publishEvent(event);
    }
}
